/** 
 * @description 具备CRUD 操作的列表页面 ，在Rsd.view.ListDialog 基础上添加modelType和schema属性
 * @description BizController 配合使用，有规定的参数格式
 * */
Rsd.define('Rsd.zen.dialog.BizFormDialog', {
    extend: 'Rsd.zen.dialog.FormDialog',
    requires: [
        'Rsd.control.SearchToolBar',
        'Rsd.form.ComboBoxEnum'
    ],
    xtype: 'biz-form-dialog',
    readonly: false,
    /**
     * @description 对象模型，用户于取Rsd.data.ModelSchema(集成了模型结构以及对象的CURD操作)对象
     * @description 优先从页面中获取，不存在时，从该页面菜单中获取
     */
    //modelType:'',
    /**
     * Rsd.cqrs.Form 对象
     */
    //handler:null,
    /**
     * 设计模式 处理方法
     */
    designHandler:null,
    /**
    * 
    * */
    constructor: function BizFormDialog(config) {
        config = config || {};
        this.apply(config);

        this.menu = this.menu||{};  
        this.readonly = this.menu.readonly == undefined ? this.readonly : this.menu.readonly;
        this.title = this.title || this.menu.title; 
        this.modelType = this.modelType || this.menu.modelType;
        this.key = this.key || this.menu.keyValue||'';
    },
      /**
     * 
     */
    onBeforeInit:function onBeforeInit()
    {
        var me = this; 
        if(me.modelSchema)
        {
            me.callParentFn('onBeforeInit');
            return;
        }
        if(!me.modelType)
        {
            me.callParentFn('onBeforeInit');
            return;
        } 
        me.schemaIsLoading = true; 
        //handler 是cqrs 对象 
        me.handler.getSchema(function(modelSchema){
           
            if(modelSchema==null)
            {
                alert('未获取到schema：' + me.handler.modelType);
                return;
            }

            me.schemaIsLoading = false;
            me.modelSchema = modelSchema;
            //设计模式参数设置
            if (me.mode == 'design') {
                me.modelSchema.fields.map((p) => {
                    p['design'] = true;
                    p['designHandler'] = function (field) {
                        if (me.designHandler) {
                            me.funApplyByIOC(me.designHandler, ["field", field])
                        }
                        else {
                            alert('属性designHandler未设置');
                        }
                    };
                });

            }
            //console.log(me.modelSchema.fields);
            var _fields = me.formFields || [];
            if (_fields.length > 0) {
                return;
            }

            try {

                _fields = modelSchema.getFields();
                me.log(_fields);
                var _hiddens = [];
                _hiddens.push({ xtype: 'hidden', label: false, name: 'modelType', value: me.modelType });
                _hiddens.push({ xtype: 'hidden', label: false, name: '___key', value: Rsd.getAppToken() });
                //合并
                me.formFields = Rsd.arrayConcat(_fields, _hiddens);

            } catch (ex) {
                Rsd.error('加载模型结构失败', ex);
            }
            //加载字段,加载完字段，后会自动加载数据
            me.loadFormFields();
                 
        });

        //设计模式参数设置
        if (me.mode == 'design') {
            me.buttons.push({
                xtype:'link',
                label:false,
                text:'添加按钮',
                width:'80px',
                style:{
                    cursor:'pointer',  
                    lineHeight: 25,
                    borderRadius: '40px',
                    backgroundColor: '#0c25eccc',
                    color: '#FFF',
                    textAlign: 'center'
                },
                handler:function(){
                    //
                    if(me.designHandler)
                    {
                        me.funApplyByIOC(me.designHandler,["button"])
                    }
                    else
                    {
                        alert('属性designHandler未设置');
                    }
                    
                }
            });
        }
          
        me.callParentFn('onBeforeInit');
    },
    /**
    *
    * */
    onBeforeRender: function onBeforeRender() {
 
        //debugger; 
        var me = this; 
        me.callParentFn('onBeforeRender');
        return this;
    },
    /*
    *
    * */
    load: function load(data) {

        var me = this; 
        if(data||this.data)
        {
             me.callParentFn("load", [data||this.data]);
        }
        else
        {
            me.handler.get(function(resp){
                me.callParentFn("load", [resp.data||{}]);
            });
        }  
        return this;
    },
    /**
     * 
     * @returns 
     */
    save:function save()
    {
        var me = this; 
        var rs = this.callParentFn("save");
         
       if(rs===false)
       {
           return false;
       }
       
        if(!me.formAction && !me.saveHandler && !me.saveServiceName)
        {
            //使用cqrs 
            me.handler.save(function (resp) { return true; }); 

            return false;
        } 
        //console.log('no action to do in bizFormDialog');
        return true;
    } 

}, function (type) {

    //modelType
    var _modelTypeGetter = function () {
 
        return this.__modelType;
    };
    var _modelTypeSetter = function (value) {

        this.__modelType = value;

    };
    //biz.list 接口参数modelType
    this.defineProperty(type, "modelType", _modelTypeGetter, _modelTypeSetter, false);
     
    var _handlerGetter = function () {
          
        if(!this.__handler)
        {
            this.__handler = Rsd.create('Rsd.cqrs.Form',{owner:this});
        }
        return this.__handler;

    };

    var _handlerSetter = function (value) {

        if(Rsd.isString(value))
        {
            this.__handler = Rsd.widget(value,{owner:this});;
            return ;
        }
        this.__handler = value;
    }

    this.defineProperty(type,"handler", _handlerGetter, _handlerSetter,true);

});